/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FindDialog.java
 *
 * Created on May 22, 2010, 11:47:39 PM
 */

package org.hbaseexplorer.components;

import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.hbaseexplorer.domain.FilterModel;
import org.hbaseexplorer.domain.Table;
import org.hbaseexplorer.exception.ExplorerException;
import org.jdesktop.application.Action;

/**
 *
 * @author zaharije
 */
public class FilterDialog extends javax.swing.JDialog {

    private FilterModel filterModel;
    private Table table;

    /** Creates new form FindDialog */
    public FilterDialog(java.awt.Frame parent, Table table) {
        super(parent, true);
        this.filterModel = new FilterModel();
        this.table = table;
        initComponents();
        fillData();
    }

    private void fillData() {
        try {
            HColumnDescriptor[] descriptors = table.getHTable().getTableDescriptor().getColumnFamilies();
            DefaultComboBoxModel model = new DefaultComboBoxModel();
            model.addElement("");
            for(HColumnDescriptor desc : descriptors) {
                model.addElement(desc.getNameAsString());
            }
            comboFamily.setModel(model);
        } catch (IOException ex) {
            throw new ExplorerException("Error getting column families.");
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        comboFamily = new javax.swing.JComboBox();
        textColumn = new javax.swing.JTextField();
        textValue = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.hbaseexplorer.HBaseExplorerApp.class).getContext().getResourceMap(FilterDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(2, 3));

        jLabel1.setLabelFor(comboFamily);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1);

        jLabel2.setLabelFor(textColumn);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel2.add(jLabel2);

        jLabel3.setLabelFor(textValue);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3);

        comboFamily.setName("comboFamily"); // NOI18N
        jPanel2.add(comboFamily);

        textColumn.setText(resourceMap.getString("textColumn.text")); // NOI18N
        textColumn.setName("textColumn"); // NOI18N
        jPanel2.add(textColumn);

        textValue.setText(resourceMap.getString("textValue.text")); // NOI18N
        textValue.setName("textValue"); // NOI18N
        jPanel2.add(textValue);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.hbaseexplorer.HBaseExplorerApp.class).getContext().getActionMap(FilterDialog.class, this);
        jButton1.setAction(actionMap.get("btnApply")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .add(jButton1)
                    .add(jLabel6))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(14, 14, 14)
                .add(jButton1)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void btnApply() {
        filterModel.setFamily(comboFamily.getSelectedItem().toString());
        filterModel.setColumn(textColumn.getText());
        filterModel.setValue(textValue.getText());
        dispose();
    }

    public FilterModel getFilterModel() {
        return filterModel;
    }

    public void setFilterModel(FilterModel filterModel) {
        this.filterModel = filterModel;
        comboFamily.setSelectedItem(filterModel.getFamily());
        textColumn.setText(filterModel.getColumn());
        textValue.setText(filterModel.getValue());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboFamily;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField textColumn;
    private javax.swing.JTextField textValue;
    // End of variables declaration//GEN-END:variables

}
